package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.BaseResult;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;



/**
 * 商品三级分类
 *
 * @author rose
 * @email zichuanhaoyue@gmail.com
 * @date 2021-05-16 19:10:47
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     *  查询出所有分类以及子分类, 以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public BaseResult list(){

        List<CategoryEntity> entities =  categoryService.listWithTree();

        return BaseResult.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public BaseResult info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return BaseResult.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public BaseResult save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return BaseResult.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public BaseResult update(@RequestBody CategoryEntity category){

		categoryService.updateById(category);

        return BaseResult.ok();
    }

    /**
     *  @RequestBody 获取请求体，必须发送POST 请求
     *
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public BaseResult delete(@RequestBody Long[] catIds){

        // 1.检查当前 删除的菜单，是否被 别的地方引用
		// categoryService.removeByIds(Arrays.asList(catIds));

        categoryService.removeMenuByIds(Arrays.asList(catIds));


        return BaseResult.ok();
    }

}
